package controllers;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import libs.Utils;
import libs.Validation;
import models.Group;
import models.User;
import models.forms.GroupForm;
import play.data.Form;
import play.data.validation.ValidationError;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.admin.accesses;
import views.html.admin.groups;
import business.GroupBusiness;
import business.UserBusiness;

public class Admin extends Controller {
	public static Result accesses() {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		List<Class<? extends Object>> arrController = Utils.getAllClasses("controllers");
		for (Class<? extends Object> controller : arrController) {
			if("Controller".equals(controller.getSuperclass().getSimpleName())){
				System.out.println("Controller: " + controller.getSimpleName());
				ArrayList<String> methods = new ArrayList<String>();
				Method[] arrMethod = controller.getDeclaredMethods();
				for (int i = 0; i < arrMethod.length; i++) {
					if("Result".equals(arrMethod[i].getReturnType().getSimpleName())){
						methods.add(arrMethod[i].getName());
					}
				}
				map.put(controller.getSimpleName(), methods);
			}
		}
		
		return ok(accesses.render(map, "Your new application is ready.", "Your new application is ready."));
	}
	
	public static Result groups(int page){
		Form<GroupForm> groupForm = Form.form(GroupForm.class);
		if (Validation.isPost()) {
			Form<GroupForm> filledForm = groupForm.bindFromRequest();
			try {
				// Check empty group name
				if (Validation.isEmpty(filledForm.field("groupName").value().trim())) {
					filledForm.reject("groupName", "You must input the group name!");
				}
				
				if (filledForm.hasErrors()) {
					for (String keyError : filledForm.errors().keySet()) {
						List<ValidationError> lstErrors = filledForm.errors().get(keyError);
						for (ValidationError error : lstErrors) {
							System.out.println("Error " + keyError + ": " + error.message().toString());
						}
					}
					GroupBusiness groupBusiness = new GroupBusiness();
					List<Group> groupList = groupBusiness.selectList(null);
					UserBusiness userBusiness = new UserBusiness();
					List<User> userList = userBusiness.selectAll();
					return badRequest(groups.render(groupList, userList, groupForm, "Alo", "Alo"));
				} else {
					GroupForm form = filledForm.get();
					for(int i=0; i<form.getGroupUsers().size(); i++){
						System.out.println("user " + i + ": "+ form.getGroupUsers().get(i));
					}
					GroupBusiness userBusiness = new GroupBusiness();
					userBusiness.save(form);
				}
			}catch(Exception e){
				e.printStackTrace(System.out);
			}
		}
		GroupBusiness groupBusiness = new GroupBusiness();
		List<Group> groupList = groupBusiness.selectAll();
		UserBusiness userBusiness = new UserBusiness();
		List<User> userList = userBusiness.selectAll();
		return ok(groups.render(groupList, userList, groupForm, "Alo", "Alo"));
	}
}
